package edu.ntu.cloudroid.nongui.util;

import java.util.logging.Logger;

import edu.ntu.cloudroid.server.RequestServlet;

public class TimeCounter {
	private int counter;
	private int completed;
	private long start;
	private String description;
	private Logger logger;
	
	public TimeCounter(String des, Logger logger) {
		counter = 0;
		completed = 0;
		start = 0;
		this.description = des;
		this.logger = logger;
	}

	public synchronized long start(boolean b) {
		long time = System.currentTimeMillis();
		if (1000 <= (time - start)) {
			if (b)
				this.logger.info(this.description+" completed = "+completed);
			else
				this.logger.info(this.description+" completed = "+completed);
			counter = completed = 0;
		}
		if (0 == counter++) {
			start = time;
		}
		return time;
	}
	
	public synchronized long end(boolean b) {
		completed++;
		long time = System.currentTimeMillis();
		if (1000 <= (time - start)) {
			if (b)
				this.logger.info(this.description+" completed = "+completed);
			else
				this.logger.info(this.description+" completed = "+completed);
			counter = completed = 0;
		}
		return time;
	}
}
